package fr.alesia.deepstack.servlet;

import java.io.IOException;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.alesia.deepstack.bean.Player;
import fr.alesia.deepstack.data.PlayerDao;
import fr.alesia.deepstack.factory.DaoFactory;
import fr.alesia.deepstack.factory.ServiceFactory;
import fr.alesia.deepstack.stat.GeneralStatistics;
import fr.alesia.deepstack.stat.PlayerStatistics;

@SuppressWarnings("serial")
public class StatisticServlet extends HttpServlet {
	
	@Override
	protected void doGet(
		HttpServletRequest request,
		HttpServletResponse response
	) throws ServletException, IOException {
		
		ServletContext context = this.getServletContext();
		PlayerDao playerDao = DaoFactory.getPlayerDao();

		String id = request.getParameter("player-id");
		
		if(id != null && id.length() > 0){
			
			Player player = playerDao.find(id);
			if(player != null){
				
				PlayerStatistics playerstat = ServiceFactory.getPlayerStatistics(player);
				request.setAttribute("playerstat", playerstat);
			}
		}
		
		GeneralStatistics statistics = ServiceFactory.getGeneralStatistics();
		request.setAttribute("statistics", statistics);
		
		Set<Player> players = playerDao.findAll();
		request.setAttribute("players", players);
		
		RequestDispatcher dispatcher = context.getRequestDispatcher("/webapp/page/private/statistics.jsp");
		dispatcher.forward(request, response);
	}

	@Override
	protected void doPost (
		HttpServletRequest request,
		HttpServletResponse response
	) throws ServletException, IOException {
		
		this.doGet(request, response);
	}
}
